/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.glorystone.ins.util;

import com.glorystone.ins.dao.DictionaryMapper;
import com.glorystone.ins.domain.Dictionary;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * 字典工具类
 * @author ThinkGem
 * @version 2013-5-29
 */
public class DictUtils {
	
	private static DictionaryMapper dictDao = SpringContextHolder.getBean(DictionaryMapper.class);


	public static String getDictValue(String key, String type, String defaultValue){
		if (StringUtils.isNotBlank(type) && StringUtils.isNotBlank(key)){
			for (Dictionary dict : getDictList(type)){
				if (type.equals(dict.getType()) && key.equals(dict.getRiskKey())){
					return dict.getRiskValue();
				}
			}
		}
		return defaultValue;
	}
	
	public static String getDictValues(String keys, String type, String defaultValue){
		if (StringUtils.isNotBlank(type) && StringUtils.isNotBlank(keys)){
			List<String> valueList = Lists.newArrayList();
			for (String value : StringUtils.split(keys, ",")){
				valueList.add(getDictValue(value, type, defaultValue));
			}
			return StringUtils.join(valueList, ",");
		}
		return defaultValue;
	}

	public static String getDictKey(String value, String type, String defaultLabel){
		if (StringUtils.isNotBlank(type) && StringUtils.isNotBlank(value)){
			for (Dictionary dict : getDictList(type)){
				if (type.equals(dict.getType()) && value.equals(dict.getRiskKey())){
					return dict.getRiskValue();
				}
			}
		}
		return defaultLabel;
	}
	
	public static List<Dictionary> getDictList(String type){
		return dictDao.getDictionaryListByType(type);
	}

}
